package com.zqh.irmadscore.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zqh.irmadscore.mapper.FailureListMapper;
import com.zqh.irmadscore.model.entity.FailureList;
import com.zqh.irmadscore.service.FailureListService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: zqh
 * @description:
 * @date: 2025/3/24 16:01
 */
@Service
public class FailureListServiceImpl extends ServiceImpl<FailureListMapper, FailureList>
        implements FailureListService {
    @Override
    public List<FailureList> getFailureList(MultipartFile file) throws IOException {

        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());

        // 读取所有行数据（默认读取第一个 Sheet）
        List<List<Object>> rows = reader.read();

        List<FailureList> list = new ArrayList<>();

        int t = 0;
        for (List<Object> row : rows){

            if(t != 0){

                String failureCode = row.get(1).toString();

//                System.out.println(failureCode);

                LambdaQueryWrapper<FailureList> queryWrapper =  new LambdaQueryWrapper<>();
                queryWrapper.eq(FailureList::getFaultCode, failureCode);

                FailureList failureList = getOne(queryWrapper);

                if(ObjectUtil.isNotEmpty(failureList)) list.add(failureList);

            }
            t++;

        }

        return list;
    }
}
